/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Common;

import Constants.Constant;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.stream.*;

/**
 *
 * @author Hmong
 */
public class LanguageManager {
    private static LanguageManager _Instance;

    private int _RecordId = 0;
    private static String LanguageFile_Path = "/midp/Whois/Languages/";
    private static String LanguageFile_Extension = ".xml";

    private static String Language_Default = "en-US";
    private String _Language_Current = null;

    protected LanguageManager(){
        
    }

    private static LanguageManager GetInstance(){
        return _Instance;
    }

    public static void Init(){
        _Instance = new LanguageManager();
    }

    public static int GetRecordId(){
        return GetInstance()._RecordId;
    }
    
    private static String GetLanguageFilePath(){
        return LanguageFile_Path;
    }

    public static String GetDefaultLanguage(){
        return Language_Default;
    }

    public static String GetCurrentLanguage(){
        return GetInstance()._Language_Current;
    }

    public static void SetCurrentLanguage(String lang){
        // 如果设置的语言存在对应的Xml Language文件，则使用设置的语言
        if ((null != lang) && IsExistsLanguageXml(lang)){
            GetInstance()._Language_Current = lang;
            return;
        }

        // 获取当前手机本地语言
        lang = System.getProperty("microedition.locale");

        // 如果成功获取当前手机本地语言，且存在对应的Xml Language文件，则使用当前手机本地语言
        if ((null != lang) && IsExistsLanguageXml(lang)){
            GetInstance()._Language_Current = lang;
            return;
        }

        // 否则使用默认语言
        lang = GetDefaultLanguage();
        GetInstance()._Language_Current = lang;
    }
    
    private static String GetLanguageFileExtension(){
        return LanguageFile_Extension;
    }

    public static boolean IsExistsLanguageXml(String lang){
        String langXml = GetLanguageFilePath() + lang + GetLanguageFileExtension();
        InputStream is = GetInstance().getClass().getResourceAsStream(langXml);
        if (null != is) {
            try {
                is.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return true;
        }

        return false;
    }

    public static void ww(){
        String langXml = GetLanguageFilePath() + GetCurrentLanguage() + GetLanguageFileExtension();
        InputStream is = LanguageManager.class.getResourceAsStream(langXml);

        XMLInputFactory factory = XMLInputFactory.newInstance();

        try {
            XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(is);

            while(xmlStreamReader.hasNext())
            {
                int nEvent = xmlStreamReader.next();
                if (nEvent == XMLStreamReader.START_ELEMENT){
                    String strCurElement = xmlStreamReader.getLocalName();
                    if ("Forms".equalsIgnoreCase(strCurElement)){

                    }
                    
                    System.out.println(strCurElement);
                }
            }

            xmlStreamReader.close();

        } catch (XMLStreamException ex) {
            ex.printStackTrace();
        }
    }
}
